home *** CD-ROM | disk | FTP | other *** search
- Path: news.spies.com!usenet
- From: Erik Max Francis <max@alcyone.com>
- Newsgroups: comp.lang.c++
- Subject: Re: 2 D arrays in C++ constructors
- Date: Sun, 31 Mar 1996 20:24:37 -0800
- Organization: Alcyone Systems
- Message-ID: <315F5A85.2E0DCBCF@alcyone.com>
- References: <4jlcn2$kvl@csugrad.cs.vt.edu>
- NNTP-Posting-Host: newton.alcyone.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.01 (X11; I; Linux 1.2.13 i486)
-
- Raj Suri wrote:
-
- > This works fine. However, now I need to use a two dimensional array in
- > which I don't know the dimensions until the constructor is called. I can't
- > figure out how to declare ptr and allocate space to it such that I could do the
- > following statement:
- >
- > ptr[2][3] = 4;
-
- There's a few ways to do it. The simplest is something like this (with X and Y
- being the size of the two-dimensional array):
-
- // First, allocate the first coordinate.
- int **base = new int *[X];
-
- // Then for each first coordinate, allocate the corresponding second.
- for (int x = 0; x < X; x++)
- base[x] = new int[Y];
-
- Then initialize the values as appropriate.
-
- --
- Erik Max Francis &tSftDotIotE && http://www.alcyone.com/max && max@alcyone.com
- San Jose, California, U.S.A. && 37 20 07 N 121 53 38 W && the 4th R is respect
- H.3`S,3,P,3$S,#$Q,C`Q,3,P,3$S,#$Q,3`Q,3,P,C$Q,#(Q.#`-"C`- && 1love && folasade
- Omnia quia sunt, lumina sunt. && Dominion, GIGO, GOOGOL, Omega, Psi, Strategem
- "Out from his breast/his soul went to seek/the doom of the just." -- _Beowulf_
-